<?php
namespace App\Controller;

use Think\Controller;

class PublicController extends Controller {

    public function getCode(){
        $oldTime = session('sms_password_time');
        if($oldTime && (strtotime(date('Y-m-d H:i:s')) - $oldTime) < 60){
            $this->error('验证码发送过于频繁，请休息一下');
        }
        $phone = I('post.phone');
        //验证格式
        if(!preg_match("/^1[34578]{1}\d{9}$/",$phone)){
            $this->error('手机号格式错误');
            return;
        }

        //查询数据库并重置密码
        $user = M('User')->where('Phone='.$phone)->find();
        if($user){
            $code = rand(100000,999999);
            session('sms_password_code',$code);
            session('sms_password_phone',$phone);

            //发送验证码
            $content = Array(  // 短信模板中字段的值
                "name" => $user['TrueName'] ? $user['TrueName'] : $user['UserName'],
                "pass" => $code
            );
            $template = 'SMS_107125106';

            $result = sendSMS($phone,$content,$template,2);

            //设置session，一分钟以后才能再次发送
            session('sms_password_time',strtotime(date('Y-m-d H:i:s')));
            if($result){
                $this->success($result);
            }else{
                $this->error('短信发送失败！');
            }
        }else{
            $this->error('手机号不存在!');
        }
    }

    /**
     * 重置密码
     */
    public function resetPassword(){
        $phone = session('sms_password_phone');
        $code = session('sms_password_code');
        $getCode = I('code');
        $getPassword = I('password');
        if($code && intval($getCode) === intval($code)){
            $user = M('User')->where(array('Phone' => $phone,'IsDel' => 0))->find();
            if($user){
                $user['Password'] = sp_password($getPassword,'',$user['UserName']);
                $this->storeLogin($user);
                $result = M('User')->save($user);
                $this->success('重置密码成功');
            }else{
                $this->error('手机号不存在');
            }
        }else{
            $this->error('验证码输入错误');
        }


        session('sms_password_code',null);
        session('sms_password_phone',null);
        session('sms_password_time',null);

        //查询数据库并重置密码
        $user = M('User')->where('Phone='.$phone)->find();
        if($user){
            $code = rand(100000,999999);
            session('sms_password_code',$code);
            session('sms_password_phone',$phone);

            //发送验证码
            $content = Array(  // 短信模板中字段的值
                "name" => $user['TrueName'] ? $user['TrueName'] : $user['UserName'],
                "pass" => $code
            );
            $template = 'SMS_107125106';

            $result = sendSMS($phone,$content,$template,2);

            //设置session，一分钟以后才能再次发送
            session('sms_password_time',strtotime(date('Y-m-d H:i:s')));
            if($result){
                $this->success($result);
            }else{
                $this->error('短信发送失败！');
            }
        }else{
            $this->error('手机号不存在!');
        }
    }

    /**
     * 登录
     */
    public function login(){
        $UserName = I('UserName');
        $Password = I('Password');
        $where['UserName'] = $UserName;
        $where['IsDel'] = array('neq','1');
        $user = M('User')->where($where)->find();
        if($user){
            if($user['Password'] === sp_password($Password,'',$UserName)){
                $userInfo = $this->storeLogin($user);
                $this->success($userInfo);
            }else{
                $this->error('用户名或密码错误');
            }
        }else{
            $this->error('用户名或密码错误');
        }
    }

    /**
     * 存储用户登录信息
     * @param $user
     * @return array
     */
    public function storeLogin($user){
        session('USER_ID',$user["ID"]);
        $userPassStr = $user['UserName'].','.$user['Password'];
        $token = encode($userPassStr);
        $TrueName = !empty($user['TrueName'])?$user['TrueName']:$user['UserName'];
        $userInfo = array('ID'=>$user['ID'],'TrueName'=>$TrueName,'token'=>$token);
        return $userInfo;
    }
}